﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;

namespace DnPakExplorer.Forms {
	public partial class ItemEdtorForm : FormBase {

		IUserItem mItem;
		bool mEnchantUpdate = false;
		bool mPotencial1Update = false;
		bool mPotencial2Update = false;
		bool mNumPetLevelUpdate = false;

		public ItemEdtorForm(IUserItem item) {
			mItem = item;
			InitializeComponent();
			labelName.Text = item.name;
			labelName.ForeColor = Util.GetRankColor(item.rank);
			labelLevelLimit.Text = item.limitLevel+"以上";
			labelJobClass.Text = item.needJobClassName;
			itemIconBox1.Item = item;

			#region 强化属性
			List<EnchantRecord> enchantProperty = item.EnchantProperty;
			if (enchantProperty != null && enchantProperty.Count>0) {
				int maxLevel = 0;
				for (int i=0; i<enchantProperty.Count; i++) {
					maxLevel = Math.Max(maxLevel, enchantProperty[i].Level);
				}
				numEnchant.Minimum = 0;
				numEnchant.Maximum = maxLevel;
				numEnchant.Value = Clamp(item.appliedEnchantLevel, 0, maxLevel);
				if (!mEnchantUpdate) {
					numEnchant_ValueChanged(null, null);
				}
			}
			panelEnchant.Visible = (enchantProperty != null && enchantProperty.Count>0);
			#endregion

			#region 制作属性
			List<PotencialRecord> potential1 = item.PotentialProperty1;
			if (potential1!=null && potential1.Count>0) {
				numPotencial1.Minimum = 0;
				numPotencial1.Maximum = potential1.Count;
				int index = potential1.FindIndex(p => p.Key == mItem.appliedPotential1)+1;
				numPotencial1.Value = Clamp(index, 0, potential1.Count);
				if (!mPotencial1Update) {
					numPotencial1_ValueChanged(null, null);
				}
			}
			panelPotencial1.Visible = (potential1 != null && potential1.Count>0);
			#endregion

			#region 附魔属性
			List<PotencialRecord> Potential2 = item.PotentialProperty2;
			if (Potential2!=null && Potential2.Count>0) {
				numPotencial2.Minimum = 0;
				numPotencial2.Maximum = Potential2.Count;
				int index = Potential2.FindIndex(p => p.Key == mItem.appliedPotential2) + 1;
				numPotencial2.Value = Clamp(index, 0, Potential2.Count);
				if (!mPotencial2Update) {
					numPotencial2_ValueChanged(null, null);
				}
			}
			panelPotencial2.Visible = (Potential2 != null && Potential2.Count>0);
			#endregion

			#region 宠物属性
			if (item.petLevelType >=0) {
				panelPetGrowLevel.Visible = true;
				numPetLevel.Minimum = 0;
				numPetLevel.Maximum = item.petMaxLevel;
				numPetLevel.Value = Clamp(item.petLevel, (int)numPetLevel.Minimum, (int)numPetLevel.Maximum);
				if (!mNumPetLevelUpdate) {
					numPetLevel_ValueChanged(null, null);
				}
			} else
				panelPetGrowLevel.Visible = false;
			#endregion

			if (item.imprintingType != ImprintingType.None) {
				panelImprint.Visible = true;
				OnImprintChanged();
			} else {
				panelImprint.Visible = false;
			}

			if (item.AppliedSlotsList != null && item.AppliedSlotsList.Count>0) {
				panelDragonJewelSlots.Visible = true;
				dragonJewelSlotsCtrl1.SetItem(item);
			} else
				panelDragonJewelSlots.Visible = false;
		}
		

		private void numEnchant_ValueChanged(object sender, EventArgs e) {
			mItem.appliedEnchantLevel = (int)numEnchant.Value;
			mEnchantUpdate = true;
			string text = "";
			List<Property> list = mItem.AppliedEnchantProperty;
			if (list!=null && list.Count>0) {
				for (int i=0; i<list.Count; i++) {
					text += InfoOverlayForm.GetPropertyText(list, ref i);
				}
			}
			if (text == "") {
				text = "无属性";
			}
			labelEnchantProperty.Text = text;
			itemIconBox1.ItemPropertyChanged = true;
		}

		private void numPotencial1_ValueChanged(object sender, EventArgs e) {
			mPotencial1Update = true;
			int index = (int)numPotencial1.Value-1;
			string text = "";
			if (index >= 0) {
				mItem.appliedPotential1 = mItem.PotentialProperty1[index].Key;
				PotencialRecord potencial = mItem.AppliedPotential1Property;
				if (potencial!=null && potencial.Properties.Count>0) {
					for (int i=0; i<potencial.Properties.Count; i++) {
						text += InfoOverlayForm.GetPropertyText(potencial.Properties, ref i);
					}
				}
				if (potencial != null && potencial.SkillId>0) {
					text += InfoOverlayForm.GetSkillText(potencial);
				}
			} else {
				mItem.appliedPotential1 = -1;
				text = "请选择制作属性";
			}
			if (text == "") {
				text = "无属性";
			}
			labelPotencial1.Text = text;
			itemIconBox1.ItemPropertyChanged = true;
		}

		private void numPotencial2_ValueChanged(object sender, EventArgs e) {
			mPotencial2Update = true;
			int index = (int)numPotencial2.Value-1;
			string text = "";
			if (index >= 0) {
				mItem.appliedPotential2 = mItem.PotentialProperty2[index].Key;
				PotencialRecord potencial = mItem.AppliedPotential2Property;
				if (potencial!=null && potencial.Properties.Count>0) {
					for (int i=0; i<potencial.Properties.Count; i++) {
						text += InfoOverlayForm.GetPropertyText(potencial.Properties, ref i);
					}
				}
				if (potencial != null && potencial.SkillId>0) {
					text += InfoOverlayForm.GetSkillText(potencial);
				}
			} else {
				mItem.appliedPotential2 = -1;
				text = "请选择附魔属性";
			}
			if (text == "") {
				text = "无属性";
			}
			labelPotencial2.Text = text;
			itemIconBox1.ItemPropertyChanged = true;
		}

		private void numPetLevel_ValueChanged(object sender, EventArgs e) {
			mNumPetLevelUpdate = true;
			mItem.petLevel = (int)numPetLevel.Value;
			string text = "";
			if (mItem.petLevel > 0) {
				List<Property> list = mItem.AppliedGrowProperty;
				if (list!=null && list.Count>0) {
					for (int i=0; i<list.Count; i++) {
						text += InfoOverlayForm.GetPropertyText(list, ref i);
					}
				}
			} else {
				mItem.appliedPotential2 = -1;
				text = "请选择宠物等级";
			}
			if (text == "") {
				text = "无属性";
			}
			labelPetProperty.Text = text;
			itemIconBox1.ItemPropertyChanged = true;
		}

		private void OnImprintChanged() {
			string text = "";
			if (mItem.AppliedImprintingSlot.Item != null) {
				IUserItem impriting = mItem.AppliedImprintingSlot.Item;
				PropertySumer sumer = new PropertySumer();
				sumer.AddItem(impriting);
				List<Property> imprintingProperties = sumer.ToList();
				for (int i=0; i<imprintingProperties.Count; i++) {
					text += InfoOverlayForm.GetPropertyText(imprintingProperties, ref i);
				}
				PotencialRecord potencial = impriting.AppliedPotential1Property;
				if (potencial != null && potencial.SkillId>0) {
					text += InfoOverlayForm.GetSkillText(potencial);
				}
			} else {
				text = "请选择刻印物品以应用刻印属性。";
			}
			if (text == "") {
				text = "无属性";
			}
			labelImprintProperty.Text = text;
			itemIconBox1.ItemPropertyChanged = true;
		}

		private static int Clamp(int value, int min, int max) {
			return value<min?min: value>max?max:value;
		}

		private void button1_Click(object sender, EventArgs e) {
			DialogResult = System.Windows.Forms.DialogResult.OK;
			Close();
		}

		private void button2_Click(object sender, EventArgs e) {
			DialogResult = System.Windows.Forms.DialogResult.Cancel;
			Close();
		}

		private void button3_Click(object sender, EventArgs e) {
			Character ch = (Character)DnPlayerUtil.CurrentCharacter;
			if (ch != null) {
				
			}
		}

		public class DragonItemUseTarget : IItemUseTarget{
			public void UseItem(BagPanel bag, IUserItem item) {

			}

		}

		public class ImprintSelectUseTarget : IItemUseTarget {

			private ItemEdtorForm mItemEdtorForm;

			public ImprintSelectUseTarget(ItemEdtorForm editor) {
				mItemEdtorForm = editor;
			}

			public void UseItem(BagPanel bag, IUserItem item) {
				if (mItemEdtorForm.mItem.AppliedImprintingSlot.CanEquip(item)) {
					mItemEdtorForm.mItem.AppliedImprintingSlot.Equip(item);
					bag.RemoveItem(item);
					mItemEdtorForm.OnImprintChanged();
				}
			}

		}

		private void buttonImprint_Click(object sender, EventArgs e) {
			BagForm form = new BagForm(DnPlayerUtil.CurrentCharacter);
			form.SetUseTarget(new ImprintSelectUseTarget(this));
			form.Show(this);
		}

	}
}
